package org.kzwl.service.app.impl;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.aliyuncvc.model.v20191030.CreateMeetingResponse;
import com.aliyuncs.aliyuncvc.model.v20191030.DeleteMeetingRequest;
import com.aliyuncs.aliyuncvc.model.v20191030.JoinMeetingResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.kzwl.comm.advice.BizException;
import org.kzwl.comm.pojo.bo.meeting.AddMeetingBO;
import org.kzwl.comm.pojo.bo.meeting.DelMeeting;
import org.kzwl.comm.pojo.bo.meeting.JoinMeetingBO;
import org.kzwl.comm.pojo.bo.meeting.QueryMeetingBasic;
import org.kzwl.comm.pojo.dto.meeting.JoinMeetingDTO;
import org.kzwl.comm.pojo.dto.meeting.MeetingBasicDTO;
import org.kzwl.comm.pojo.dto.meeting.MeetingDetailsDTO;
import org.kzwl.comm.utils.AliUtils;
import org.kzwl.comm.utils.MyUtils;
import org.kzwl.entity.app.CuMeeting;
import org.kzwl.entity.app.Feedback;
import org.kzwl.entity.app.User;
import org.kzwl.mapper.app.FeedbackMapper;
import org.kzwl.mapper.app.MeetingMapper;
import org.kzwl.mapper.app.UserMapper;
import org.kzwl.service.app.FeedbackService;
import org.kzwl.service.app.MeetingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 张国伟/郭家恒
 * @since 2020-09-10
 */
@Service
public class MeetingServiceImpl extends ServiceImpl<MeetingMapper, CuMeeting> implements MeetingService {

    @Autowired
    private MeetingMapper meetingMapper;

    @Autowired
    private UserMapper userMapper;

    //region 创建会议
    @Transactional
    @Override
    public void addMeeting(AddMeetingBO bo) {
        User user = userMapper.selectById(bo.getCuUserId());
        if (user.getCuUserMeeting() == 0) {
            throw new BizException("当前用户无发起权限");
        }
        CuMeeting meeting = new CuMeeting();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        meeting.setCuMeetingEndDate(LocalDateTime.parse(bo.getCuMeetingEndDate(), df));
        meeting.setCuMeetingStart(LocalDateTime.parse(bo.getCuMeetingStart(), df));
        meeting.setCuMeetingName(bo.getCuMeetingName());
        meeting.setCuUserId(bo.getCuUserId());
        meeting.setCuMeetingPwd(bo.getCuMeetingPwd());
        AliUtils.createUser(bo.getCuUserId().toString(), user.getCuUsername());
        CreateMeetingResponse cmr = AliUtils.createMeeting(bo.getCuUserId().toString(), bo.getCuMeetingName(), bo.getCuMeetingPwd(), true);
        meeting.setCuMeetingUuid(cmr.getMeetingInfo().getMeetingUUID());
        meeting.setCuMeetingCode(cmr.getMeetingInfo().getMeetingCode());
        meetingMapper.insert(meeting);
    }
    //endregion

    //region 查询会议
    @Override
    public List<MeetingBasicDTO> queryBasic(QueryMeetingBasic bo) {
        IPage<CuMeeting> page = new Page<>();
        page.setCurrent(bo.getCurrent()).setSize(bo.getSize());
        QueryWrapper<CuMeeting> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().gt(CuMeeting::getCuMeetingEndDate, LocalDateTime.now());
        page = meetingMapper.selectPage(page, queryWrapper);
        List<MeetingBasicDTO> list = JSONObject.parseArray(JSONObject.toJSONString(page.getRecords()), MeetingBasicDTO.class);
        list.forEach(l -> {
            User user = userMapper.selectById(l.getCuUserId());
            l.setCuUserName(user.getCuUsername());
        });
        return list;
    }
    //endregion

    //region 查询会议详情
    @Override
    public MeetingDetailsDTO queryDetails(QueryMeetingBasic bo) {
        CuMeeting meeting = meetingMapper.selectById(bo.getCuMeetingId());
        MeetingDetailsDTO dto = new MeetingDetailsDTO();
        BeanUtils.copyProperties(meeting, dto);
        User user = userMapper.selectById(dto.getCuUserId());
        dto.setCuUserName(user.getCuUsername());
        return dto;
    }
    //endregion

    //region 加入会议
    @Override
    public JoinMeetingDTO joinMeeting(JoinMeetingBO bo) {
        AliUtils.createUser(bo.getCuUserId(), bo.getCuMeetingName());
        JoinMeetingResponse joinMeetingResponse = AliUtils.joinMeeting(bo.getCuMeetingCode(), bo.getCuUserId(), bo.getCuMeetingPwd());
        JoinMeetingDTO dto = new JoinMeetingDTO();
        BeanUtils.copyProperties(joinMeetingResponse.getMeetingInfo(), dto);
        return dto;
    }
    //endregion

    //region 删除会议
    @Override
    public void delMeeting(DelMeeting bo) {
        CuMeeting meeting = meetingMapper.selectById(bo.getCuMeetingId());
        if (meeting.getCuUserId().longValue() != bo.getCuUserId().longValue()) {
            throw new BizException("用户权限不足");
        }
        meeting.setCuMeetingIsDel(1);
        AliUtils.deleteMeeting(meeting.getCuMeetingUuid());
        meetingMapper.updateById(meeting);
    }
    //endregion

    //region 快速会议
    @Override
    public JoinMeetingDTO quickMeeting(AddMeetingBO bo) {
        User user = userMapper.selectById(bo.getCuUserId());
        if (user.getCuUserMeeting()==0){
            throw new BizException("权限不足");
        }
        CuMeeting meeting = new CuMeeting();
        //开始时间为当前时间，结束时间咱订俩小时
        meeting.setCuMeetingEndDate(LocalDateTime.now().plusHours(2));
        meeting.setCuMeetingStart(LocalDateTime.now());
        meeting.setCuUserId(user.getCuUserId());
        meeting.setCuMeetingPwd(bo.getCuMeetingPwd());
        meeting.setCuMeetingName(user.getCuUsername() + "的快速会议");
        AliUtils.createUser(meeting.getCuUserId().toString(), user.getCuUsername());
        CreateMeetingResponse cmr = AliUtils.createMeeting(meeting.getCuUserId().toString(), meeting.getCuMeetingName(), meeting.getCuMeetingPwd(), true);
        meeting.setCuMeetingUuid(cmr.getMeetingInfo().getMeetingUUID());
        meeting.setCuMeetingCode(cmr.getMeetingInfo().getMeetingCode());
        meetingMapper.insert(meeting);
        JoinMeetingBO join = new JoinMeetingBO();
        join.setCuMeetingCode(meeting.getCuMeetingCode());
        join.setCuMeetingName(user.getCuUsername());
        join.setCuMeetingPwd(meeting.getCuMeetingPwd());
        join.setCuUserId(user.getCuUserId() + "");
        JoinMeetingDTO joinMeetingDTO = this.joinMeeting(join);
        return joinMeetingDTO;
    }
    //endregion

}
